package it.prova.action;

import it.prova.Consts;
import it.prova.Utility;
import it.prova.dialog.MyDialog;
import nu.xom.Document;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.w3c.dom.Element;

public class MultiDataUnitAction extends Action
{
	private Document currentModel;
	
	private Element element;
	
	public MultiDataUnitAction()
	{
		setText("HTML Editor");
	}
	
	/**
	 * @return Returns the element.
	 */
	public Element getElement()
	{
		return element;
	}

	/**
	 * @param element
	 *            The element to set.
	 */
	public void setElement(Element element)
	{
		this.element = element;
	}
	
	@Override
	public void run()
	{
		currentModel = Utility.getCurrentModel(Consts.MULTI_DATA_UNIT);
				
		MyDialog dialog = new MyDialog
		(
			null, 
			"Attributi disponibili", 
			element.getAttribute(Consts.TAG_ATTRIBUTE_NAME), 
			currentModel,
			element.getLocalName()
		);
		
		int result = dialog.open();
		System.out.println("result = " + result);
		String selectedAttributeContent = dialog.getSelectedAttributeContent();
		System.out.println("dialog.getSelectedAttributeContent() = " + selectedAttributeContent);			
		String selectedAttributeValue = dialog.getSelectedAttributeValue();
		System.out.println("dialog.getSelectedAttributeValue() = " + selectedAttributeValue);
		
		// write received code to .model file
		if
		(
			(result == Status.OK)
		)
		{
			System.out.println("saving...");
			
			nu.xom.Element attribute = Utility.getAttributeElement(currentModel, selectedAttributeValue);
			if(attribute != null)
			{
				Utility.manageElement(currentModel, attribute, selectedAttributeContent, element.getLocalName());
				System.out.println("currentModel.toXml(): " + currentModel.toXML());
				
				Utility.setCurrentModel(currentModel);
			}
			else
			{
				System.out.println("Cannot find a tag with '" + selectedAttributeValue + "' value for 'name' attribute inside current model");
			}
		}
		
		element.setAttribute(Consts.TAG_ATTRIBUTE_NAME, selectedAttributeValue);
	}
}